package com.antler.luajavalibrary.luaext.ui;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.antler.luajavalibrary.luaext.utils.FastJSONUtils;

public class LayoutInflater {
    protected static final String TAG = "antler_" + LayoutInflater.class.getSimpleName();

    protected final UIContext mUIContext;

    public static LayoutInflater from(UIContext uiContext) {
        return new LayoutInflater(uiContext);
    }

    public LayoutInflater(UIContext uiContext) {
        mUIContext = uiContext;
    }

    public UIView inflate(JSONObject parser, ViewGroup root) {
        View subview = null;

        if (parser != null) {
            final String typeName = parser.getString("view");
            if (typeName != null) {
                final String id = parser.getString("id");

                switch (typeName) {
                    case "div": {
                        subview = new UIDiv(mUIContext, id, parser);
                        break;
                    }
                    case "scroller": {
                        final String scrollDirection = FastJSONUtils.getString(parser, "scroll-direction", "vertical");
                        if (0 == scrollDirection.compareTo("vertical")) {
                            subview = new UIScrollerVertical(mUIContext, id, parser);
                        } else if (0 == scrollDirection.compareTo("horizontal")) {
                            subview = new UIScrollerHorizontal(mUIContext, id, parser);
                        }
                        break;
                    }
                    case "text": {
                        subview = new UIText(mUIContext, id, parser);
                        break;
                    }
                    case "input": {
                        subview = new UIInput(mUIContext, id, parser);
                        break;
                    }
                    default: {
                        Log.e(TAG, "type:" + typeName + " not support");
                        break;
                    }
                }

                if (subview != null) {
                    // 按照tag查找view
                    subview.setTag(id);

                    if (root != null) {
                        root.addView(subview);
                    }

                    if (subview instanceof ViewGroup) {
                        final JSONArray subParser = parser.getJSONArray("subviews");
                        if (subParser != null) {
                            final int size = subParser.size();
                            for (int i = 0; i < size; ++i) {
                                inflate(subParser.getJSONObject(i), (ViewGroup) subview);
                            }
                        }
                    }
                }
            }
        }
        return (UIView) subview;
    }
}
